package com.example.teesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.teesystem.common.utils.Response;
import com.example.teesystem.common.utils.ResultCode;
import com.example.teesystem.entity.ExamPaper;
import com.example.teesystem.entity.Violation;
import com.example.teesystem.entity.vo.PageResponseVo;
import com.example.teesystem.entity.vo.violation.ViolationInsertVo;
import com.example.teesystem.entity.vo.violation.ViolationSelectVo;
import com.example.teesystem.entityDto.ViolationDto;
import com.example.teesystem.filter.MyHandlerInterceptor;
import com.example.teesystem.mapper.ExamPaperMapper;
import com.example.teesystem.service.ViolationService;
import com.example.teesystem.mapper.ViolationMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 12510
* @description 针对表【t_violation】的数据库操作Service实现
* @createDate 2022-04-12 14:57:00
*/
@Service
@RequiredArgsConstructor
public class ViolationServiceImpl extends ServiceImpl<ViolationMapper, Violation>
    implements ViolationService{

    private final ViolationMapper violationMapper;
    private final ExamPaperMapper examPaperMapper;
    private static int ERROR = -1;

    @Override
    public Response insert(ViolationInsertVo violationInsertVo) {
        int id;
        try{
            id = violationMapper.getId(MyHandlerInterceptor.getUid(), violationInsertVo.getPaperId());
        }catch (Exception e){
            id = ERROR;
        }
        if(id == -1){
            int vId = ERROR;
            Violation violation = new Violation();
            violation.setPaperId(violationInsertVo.getPaperId());
            violation.setStudentId(MyHandlerInterceptor.getUid());
            violationMapper.insert(violation);
            vId = violation.getId();
            if(vId == ERROR){
                return Response.error(ResultCode.INSERT_ERROR.getMsg(), ResultCode.INSERT_ERROR.getCode());
            }
        }else {
            violationMapper.addNum(id);
        }

        return Response.ok();
    }

    @Override
    public Response getViolation(ViolationSelectVo violationSelectVo) {
        Page<Violation> violationPage = new Page<>(violationSelectVo.getCur(),violationSelectVo.getSize());
        QueryWrapper<Violation> wrapper = new QueryWrapper<>();
        wrapper.eq("paper_id",violationSelectVo.getPaperId());
        Page<Violation> myPage = this.page(violationPage,wrapper);
        List<Violation> violations = myPage.getRecords();
        List<ViolationDto> violationDtos = new ArrayList<>();
        String paperName = examPaperMapper.getNameById(violationSelectVo.getPaperId());
        for (Violation violation : violations) {
            ViolationDto violationDto = new ViolationDto(violation);
            violationDto.setPaperName(paperName);
            violationDtos.add(violationDto);
        }
        return Response.ok(new PageResponseVo<>(violationDtos, myPage.getCurrent(), myPage.getSize(), myPage.getPages()));
    }
}




